/**
 * 北京钉图互动科技 all right reserver
 */
package com.impress.commons.vo;

import java.io.Serializable;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

/**
 * @author Wangyi
 * @since 2016年6月13日
 */
@ApiModel(value = "数据分页请求")
public class PageRequest implements Serializable {

	private static final long serialVersionUID = -8548331631700923682L;

	@ApiModelProperty(value = "页号,0开始", required = true)
	private final int page;

	@ApiModelProperty(value = "页尺寸,大于0", required = true)
	private final int size;

	@ApiModelProperty(value = "排序选项", required = false)
	private final Sort sort;

	/**
	 * Creates a new {@link PageRequest}. Pages are zero indexed, thus providing
	 * 0 for {@code page} will return the first page.
	 * 
	 * @param page
	 *            must not be less than zero.
	 * @param size
	 *            must not be less than one.
	 * @param sort
	 *            can be {@literal null}.
	 */
	@JsonCreator
	public PageRequest(@JsonProperty(required = true) int page, @JsonProperty(required = true) int size,
			@JsonProperty(value = "sort", required = false) Sort sort) {
		if (page < 0) {
			throw new IllegalArgumentException("Page index must not be less than zero!");
		}

		if (size < 1) {
			throw new IllegalArgumentException("Page size must not be less than one!");
		}

		this.page = page;
		this.size = size;
		this.sort = sort;
	}

	/**
	 * Creates a new {@link PageRequest}. Pages are zero indexed, thus providing
	 * 0 for {@code page} will return the first page.
	 * 
	 * @param page
	 *            must not be less than zero.
	 * @param size
	 *            must not be less than one.
	 */
	public PageRequest(int page, int size) {
		this(page, size, null);
	}

	public int getPageSize() {
		return size;
	}

	public int getPageNumber() {
		return page;
	}

	public int getOffset() {
		return page * size;
	}

	/**
	 * @return the sort
	 */
	public Sort getSort() {
		return sort;
	}
}
